/**
  ************************************* Copyright ******************************
  * FileName   : n32l40x_it.c
  * Version    : v1.0
  * Author     : yychao
  * Date       : 2023-11-28
  * Description:
  * Function List:
  	1. ....
  	   <version>:
  <modify staff>:
  		  <data>:
   <description>:
  	2. ...
  ******************************************************************************
 */
#include "n32l40x.h"
#include "usart.h"
#include "queue.h"

/**
 * @brief  This function handles SysTick Handler.
 */
void SysTick_Handler(void)
{

}


void TIM1_UP_IRQHandler(void)
{
    if (TIM_GetIntStatus(TIM1, TIM_INT_UPDATE) != RESET)
    {
        TIM_ClrIntPendingBit(TIM1, TIM_INT_UPDATE);
    }
}


void USART2_IRQHandler(void)
{
    if (USART_GetIntStatus(USART2, USART_INT_RXDNE) != RESET) {
        if(usart2_cache.cnt < USART_CACHE_LEN) {
            usart2_cache.data[usart2_cache.cnt++] = USART_ReceiveData(USART2);
        } else {
            USART_ReceiveData(USART2);
        }
        USART_ClrIntPendingBit(USART2, USART_INT_RXDNE);
    }
    if (USART_GetIntStatus(USART2, USART_INT_IDLEF) != RESET) {
#if 0
        for(uint8_t i = 0 ; i < usart2_cache.cnt; i++) {
            USART_SendData(USART2, usart2_cache.data[i]);
            while (USART_GetFlagStatus(USART2, USART_FLAG_TXDE) == RESET)
            {
            }
        }
#else
        Write_RingBuff(&usart2_buff, &usart2_cache.data[0], usart2_cache.cnt);
#endif
        USART_ReceiveData(USART2);
        USART_ClrIntPendingBit(USART2, USART_INT_IDLEF);
        usart2_cache.cnt = 0;
    }
}

void USART3_IRQHandler(void)
{
    if (USART_GetIntStatus(USART3, USART_INT_RXDNE) != RESET) {
        if(usart3_cache.cnt < USART_CACHE_LEN) {
            usart3_cache.data[usart3_cache.cnt++] = USART_ReceiveData(USART3);
        } else {
            USART_ReceiveData(USART3);
        }
        USART_ClrIntPendingBit(USART3, USART_INT_RXDNE);
    }
    if (USART_GetIntStatus(USART3, USART_INT_IDLEF) != RESET) {
#if 0
        for(uint8_t i = 0 ; i < usart3_cache.cnt; i++) {
            USART_SendData(USART3, usart3_cache.data[i]);
            while (USART_GetFlagStatus(USART3, USART_FLAG_TXDE) == RESET)
            {
            }
        }
#else
        Write_RingBuff(&usart3_buff, &usart3_cache.data[0], usart3_cache.cnt);
#endif
        USART_ReceiveData(USART3);
        USART_ClrIntPendingBit(USART3, USART_INT_IDLEF);
        usart3_cache.cnt = 0;
    }
}
